package com.lw.tcpdemo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author LW
 * @2020/9/13 22:18
 */
public class AutoCloseIOServer {

    public static void main(String[] args) {
        try(ServerSocket serverSocket = new ServerSocket(9999);
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
            //2.等待客户端连接过来

            //3.读取客户端的消息

            byte[] buffer = new byte[1024];
            int len;
            while ((len=is.read(buffer))!=-1){
                baos.write(buffer,0,len);
            }
            //里面无论怎么乱都没事，
            //直到输出的时候其实已经写完了
            System.out.println(baos.toString());

        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
